local HJLua__qianzhan = fk.CreateSkill {

  name = "HJLua__qianzhan",

}

Fk:loadTranslationTable{

    ["HJLua__qianzhan"] = "谦战",
    [":HJLua__qianzhan"] = "出牌阶段开始时、或当你使用牌造成伤害后，你可以令你的以下值+1：攻击范围、手牌数、手牌上限。若你因此成为此项数值全场最高的角色，你对一名此项数值与你相等的角色造成1点伤害并摸两张牌，然后重置此项且本回合不能再选择此项。",
    ["#qianzhan-choice"] = "谦战：选择要增加的数值",
    ["qianzhan1"] = "攻击范围+1",
    ["qianzhan2"] = "摸一张牌",
    ["qianzhan3"] = "手牌上限+1",
    ["#qianzhan-target"] = "谦战：选择一名数值相等的角色造成伤害",
    ["@qianzhan_attack"] = "谦战范围+",
    ["@qianzhan_handlimit"] = "谦战上限+",
}


HJLua__qianzhan:addEffect(fk.EventPhaseStart, {
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(HJLua__qianzhan.name) then return false end
        return target == player and target.phase == Player.Play
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local choices = {"Cancel"}
        -- 检查哪些选项还可用
        if player:getMark("qianzhan_attack-turn") == 0 then table.insert(choices, "qianzhan1") end
        if player:getMark("qianzhan_handnum-turn") == 0 then table.insert(choices, "qianzhan2") end
        if player:getMark("qianzhan_handlimit-turn") == 0 then table.insert(choices, "qianzhan3") end
        local choice = player.room:askToChoice(player, {
            choices = choices,
            skill_name = HJLua__qianzhan.name,
            prompt = "#qianzhan-choice",
        })
        if choice ~= "Cancel" then
            event:setCostData(self, {choice = choice} )
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self).choice
        -- 根据选择增加相应数值
        if choice == "qianzhan1" then
            room:addPlayerMark(player, "@qianzhan_attack", 1)
            -- 检查是否成为最高
            local highest = false
            if table.every(room:getOtherPlayers(player), function(p) return player:getAttackRange() >= p:getAttackRange() end) then
                highest = true
            end
            if highest then
                -- 寻找攻击范围相同的角色
                local targets = table.filter(room:getOtherPlayers(player), function(p)
                    return p:getAttackRange() == player:getAttackRange()
                end)
                if #targets > 0 then
                    local to = room:askToChoosePlayers(player, {
                        targets = targets,
                        min_num = 1,
                        max_num = 1,
                        prompt = "#qianzhan-target",
                        skill_name = HJLua__qianzhan.name,
                        cancelable = false,
                    })

                    if #to > 0 then
                        room:damage({
                            from = player,
                            to = to[1],
                            damage = 1,
                            skillName = HJLua__qianzhan.name
                        })
                        room:setPlayerMark(player, "qianzhan_damage-turn", 1)
                        player:drawCards(2, HJLua__qianzhan.name)
                        room:setPlayerMark(player, "@qianzhan_attack", 0)
                        room:setPlayerMark(player, "qianzhan_attack-turn", 1)
                    end
                end
            end
        elseif choice == "qianzhan2" then
            player:drawCards(1, HJLua__qianzhan.name)
            -- 检查是否成为最高
            local highest = false
            if table.every(room:getOtherPlayers(player), function(p) return player:getHandcardNum() >= p:getHandcardNum() end) then
                highest = true
            end
            if highest then
                -- 寻找手牌数相同的角色
                local targets = table.filter(room:getOtherPlayers(player), function(p)
                    return p:getHandcardNum() == player:getHandcardNum()
                end)
                if #targets > 0 then
                    local to = room:askToChoosePlayers(player, {
                        targets = targets,
                        min_num = 1,
                        max_num = 1,
                        prompt = "#qianzhan-target",
                        skill_name = HJLua__qianzhan.name,
                        cancelable = false,
                    })
                    if #to > 0 then
                        room:damage({
                            from = player,
                            to = to[1],
                            damage = 1,
                            skillName = HJLua__qianzhan.name
                        })
                        room:setPlayerMark(player, "qianzhan_damage-turn", 1)
                        player:drawCards(2, HJLua__qianzhan.name)
                        room:setPlayerMark(player, "qianzhan_handnum-turn", 1)
                    end
                end
            end
        else
            room:addPlayerMark(player, "@qianzhan_handlimit", 1)
            -- 检查是否成为最高
            local highest = false
            if table.every(room:getOtherPlayers(player), function(p) return player:getMaxCards() >= p:getMaxCards() end) then
                highest = true
            end
            if highest then
                -- 寻找手牌上限相同的角色
                local targets = table.filter(room:getOtherPlayers(player), function(p)
                    return p:getMaxCards() == player:getMaxCards()
                end)
                if #targets > 0 then
                    local to = room:askToChoosePlayers(player, {
                        targets = targets,
                        min_num = 1,
                        max_num = 1,
                        prompt = "#qianzhan-target",
                        skill_name = HJLua__qianzhan.name,
                        cancelable = false,
                    })
                    if #to > 0 then
                        room:damage({
                            from = player,
                            to = to[1],
                            damage = 1,
                            skillName = HJLua__qianzhan.name
                        })
                        room:setPlayerMark(player, "qianzhan_damage-turn", 1)
                        player:drawCards(2, HJLua__qianzhan.name)
                        room:setPlayerMark(player, "@qianzhan_handlimit", 0)
                        room:setPlayerMark(player, "qianzhan_handlimit-turn", 1)
                    end
                end
            end
        end
    end,
})
HJLua__qianzhan:addEffect(fk.Damage, {
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(HJLua__qianzhan.name) then return false end
        return target == player and data.card
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local choices = {"Cancel"}
        -- 检查哪些选项还可用
        if player:getMark("qianzhan_attack-turn") == 0 then table.insert(choices, "qianzhan1") end
        if player:getMark("qianzhan_handnum-turn") == 0 then table.insert(choices, "qianzhan2") end
        if player:getMark("qianzhan_handlimit-turn") == 0 then table.insert(choices, "qianzhan3") end
        local choice = player.room:askToChoice(player, {
            choices = choices,
            skill_name = HJLua__qianzhan.name,
            prompt = "#qianzhan-choice",
        })
        if choice ~= "Cancel" then
            event:setCostData(self, {choice = choice} )
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self).choice
        -- 根据选择增加相应数值
        if choice == "qianzhan1" then
            room:addPlayerMark(player, "@qianzhan_attack", 1)
            -- 检查是否成为最高
            local highest = false
            if table.every(room:getOtherPlayers(player), function(p) return player:getAttackRange() >= p:getAttackRange() end) then
                highest = true
            end
            if highest then
                -- 寻找攻击范围相同的角色
                local targets = table.filter(room:getOtherPlayers(player), function(p)
                    return p:getAttackRange() == player:getAttackRange()
                end)
                if #targets > 0 then
                    local to = room:askToChoosePlayers(player, {
                        targets = targets,
                        min_num = 1,
                        max_num = 1,
                        prompt = "#qianzhan-target",
                        skill_name = HJLua__qianzhan.name,
                        cancelable = false,
                    })

                    if #to > 0 then
                        room:damage({
                            from = player,
                            to = to[1],
                            damage = 1,
                            skillName = HJLua__qianzhan.name
                        })
                        room:setPlayerMark(player, "qianzhan_damage-turn", 1)
                        player:drawCards(2, HJLua__qianzhan.name)
                        room:setPlayerMark(player, "@qianzhan_attack", 0)
                        room:setPlayerMark(player, "qianzhan_attack-turn", 1)
                    end
                end
            end
        elseif choice == "qianzhan2" then
            player:drawCards(1, HJLua__qianzhan.name)
            -- 检查是否成为最高
            local highest = false
            if table.every(room:getOtherPlayers(player), function(p) return player:getHandcardNum() >= p:getHandcardNum() end) then
                highest = true
            end
            if highest then
                -- 寻找手牌数相同的角色
                local targets = table.filter(room:getOtherPlayers(player), function(p)
                    return p:getHandcardNum() == player:getHandcardNum()
                end)
                if #targets > 0 then
                    local to = room:askToChoosePlayers(player, {
                        targets = targets,
                        min_num = 1,
                        max_num = 1,
                        prompt = "#qianzhan-target",
                        skill_name = HJLua__qianzhan.name,
                        cancelable = false,
                    })
                    if #to > 0 then
                        room:damage({
                            from = player,
                            to = to[1],
                            damage = 1,
                            skillName = HJLua__qianzhan.name
                        })
                        room:setPlayerMark(player, "qianzhan_damage-turn", 1)
                        player:drawCards(2, HJLua__qianzhan.name)
                        room:setPlayerMark(player, "qianzhan_handnum-turn", 1)
                    end
                end
            end
        else
            room:addPlayerMark(player, "@qianzhan_handlimit", 1)
            -- 检查是否成为最高
            local highest = false
            if table.every(room:getOtherPlayers(player), function(p) return player:getMaxCards() >= p:getMaxCards() end) then
                highest = true
            end
            if highest then
                -- 寻找手牌上限相同的角色
                local targets = table.filter(room:getOtherPlayers(player), function(p)
                    return p:getMaxCards() == player:getMaxCards()
                end)
                if #targets > 0 then
                    local to = room:askToChoosePlayers(player, {
                        targets = targets,
                        min_num = 1,
                        max_num = 1,
                        prompt = "#qianzhan-target",
                        skill_name = HJLua__qianzhan.name,
                        cancelable = false,
                    })
                    if #to > 0 then
                        room:damage({
                            from = player,
                            to = to[1],
                            damage = 1,
                            skillName = HJLua__qianzhan.name
                        })
                        room:setPlayerMark(player, "qianzhan_damage-turn", 1)
                        player:drawCards(2, HJLua__qianzhan.name)
                        room:setPlayerMark(player, "@qianzhan_handlimit", 0)
                        room:setPlayerMark(player, "qianzhan_handlimit-turn", 1)
                    end
                end
            end
        end
    end,
})

HJLua__qianzhan:addEffect("maxcards", {
  correct_func = function(self, player)
    return player:getMark("@qianzhan_handlimit")
  end,
})

HJLua__qianzhan:addEffect("atkrange", {
  correct_func = function (self, from, to)
    return from:getMark("@qianzhan_attack")
  end,
})

return HJLua__qianzhan